package mrms.api.web.controllers;

import lombok.AllArgsConstructor;
import mrms.api.entity.UserEntity;
import mrms.api.repository.DeptRepository;
import mrms.api.repository.UserRepository;
import mrms.api.service.UserService;
import mrms.api.web.authentication.Encipher;
import mrms.api.web.authentication.JwtUtils;
import mrms.api.web.dto.AdminUpdateUserDto;
import mrms.api.web.dto.UserRegDto;
import mrms.api.web.utils.Result;
import mrms.api.web.utils.PicUtil;
import mrms.api.web.utils.UserState;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;

/**
 * @author lenchu
 * @date 2019/02/02
 */
@RestController
@AllArgsConstructor
public class    AuthController {
    private final UserRepository userRepository;
    private final JwtUtils jwtUtils;
    private final UserService userService;
    private final DeptRepository deptRepository;

    @PostMapping("/login")
    public Result login(HttpServletRequest request) throws IOException {
        UserEntity userEntity = (UserEntity) request.getAttribute("user");
        if (userEntity == null) {
            return Result.error("登录失败");
        } else {
            Integer enabled = userEntity.getEnabled();
            if (enabled == -2) {
                return Result.error("用户已被禁用");
            } else if (enabled == -1) {
                return Result.error("用户信息尚在审核中");
            }
            userEntity.setFaceuri(PicUtil.baseurl(userEntity.getFaceuri()));
            Map<String, Object> map = new HashMap<>(4);
            map.put("Authorization",
                    jwtUtils.writeUserIdIntoJwt(userEntity.getId(), userEntity.getPassword()));
            map.put("user", userEntity);
            return Result.success(map);
        }
    }

    @PostMapping({"/reg", "/a/user"})
    public Result reg(@RequestBody @Valid UserRegDto userRegDto) {
        UserEntity userEntity = userRepository.findById(userRegDto.getId()).orElse(new UserEntity());
        BeanUtils.copyProperties(userRegDto, userEntity);
        userEntity.setEnabled(UserState.CHECK_PENDING);
        userEntity.setDept(deptRepository.findById(userRegDto.getDept()).get());
        userService.save(userEntity);
        return Result.success(userEntity);
    }

    @PutMapping("/a/user")
    public Result updateUser(@RequestBody @Valid AdminUpdateUserDto dto) {
        UserEntity userEntity = userRepository.findById(dto.getId()).get();
        BeanUtils.copyProperties(dto, userEntity);
        userEntity.setDept(deptRepository.findById(dto.getDept()).get());
        userService.save(userEntity);
        return Result.success(userEntity);
    }

    @PostMapping("/a/user/batch")
    public Result batchReg(@RequestBody @Valid List<UserRegDto> users) {
        List<UserEntity> uelist = new ArrayList<>(users.size());
        for (int i = 0; i < users.size(); i++) {
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(users.get(i), userEntity);
            userEntity.setEnabled(UserState.CHECK_PENDING);
            userEntity.setPassword(Encipher.encryptPasswordWithMD5(userEntity.getPassword()));
            userEntity.setDept(deptRepository.findById(users.get(i).getDept()).get());
            uelist.add(userRepository.save(userEntity));
        }
        return Result.success(uelist);
    }
}
